var util = require('../../utils/util.js');
var api = require('../../config/api.js');

//获取应用实例
const app = getApp();
Page({
    data: {
      userInfo: null,
      wx_code_url: '',
      show_user_btn:false,
      sort:0,
      navList:[],
        dataList: [],
        page: 1,
        size: 5,
        loadmoreText: '正在加载更多数据',
        nomoreText: '全部加载完成',
        nomore: false,
        totalPages: 1,
        FileHost:''
    },
    onLoad: function (options) {
      var that=this;
      if(options.sort)
      {
        that.setData({sort:options.sort});
      }
    this.setData({ FileHost: api.FileHost });
    var IsDebug=wx.getStorageSync('IsDebug');
    this.setData({IsDebug:IsDebug});
    this.setData({page_height:wx.getSystemInfoSync().windowHeight});

    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo != "") 
    {
      this.setData({userInfo: userInfo});
      if(userInfo.UserName=='')
      {
        this.setData({show_user_btn:true});
      }
    } 
    else 
    {      
      wx.login({
        success: res => {
          that.setData({wx_login_code:res.code});
        }
      })
    }
      this.get_sort();
       this.getList();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    onReachBottom: function () {
        this.getList()
      },
    onShareAppMessage: function () {
        return {
        title: '武汉泰祥汽配',
        desc: '装机视频',
        path: '/pages/video/index'
        }
    },
    switchCate: function (event) {
      
      var that = this;
      var sort=event.currentTarget.dataset.id;
      if (this.data.sort == event.currentTarget.dataset.id) 
      {
        return false;
      }

      that.setData({sort:sort});
      this.setData({
        sort: event.currentTarget.dataset.id,
        page:1,
        totalPages: 1,
        dataList: [],
        nomore: false
      });
       
      this.getList();
    },
    get_sort:function()
    {
      var that = this;
      util.request(api.GetSortList, {Pid: 8 },'GET').then(function (res) 
        {
          that.setData({navList: res.Data});
        });
    },
  getList:function(){
    let that = this;
    var sort=that.data.sort;
    if (that.data.totalPages <= that.data.page - 1) 
    {
      that.setData({nomore: true});
      return;
    }
    var pd={IsLive:-1,ST:-1,SortID2:sort, Page: that.data.page, PageSize: that.data.size};
    util.request(api.GetVideoList,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      {
        that.setData({
          dataList: that.data.dataList.concat(res.Data.Models),
          page: res.Data.CurrentPage + 1,
          totalPages: res.Data.TotalPage
        });
        var arr=that.data.dataList;
        var page_height=wx.getSystemInfoSync().windowHeight;
        if((arr.length)*320>wx.getSystemInfoSync().windowHeight)
        {
          page_height=(arr.length)*320;
        }
        else
        {
          page_height=wx.getSystemInfoSync().windowHeight;
        }
        that.setData({page_height:page_height});
      }
      else
      {
        wx.showToast({title: '没有数据'});
      }
    });
  },
  getPhoneAndLogin: function (e) {
    let that = this;
    var iv = e.detail.iv;
    var encryptedData = e.detail.encryptedData;
    if (e.detail.errMsg !="getPhoneNumber:ok") 
    {
      return;
    }
    wx.showLoading({mask: true});
      // 检查登录态是否过期
      wx.checkSession({
        success(res) 
        {
          var data={jscode:that.data.wx_login_code,encryptedData:encryptedData,IV:iv};
          util.request(api.GetPhoneNumber,data, 'POST').then(function (res) 
          {
            if (res.Status === 100) 
            {
              var extInfo=res.Data;
              that.setData({extInfo: extInfo});
              wx.setStorageSync('extInfo', extInfo);
              //------------------开始注册---------------------------------
              var shopid = wx.getStorageSync('ShopID') || 1;
              var uid = wx.getStorageSync('uid') || '';
              var pd = {
                OpenID: extInfo.openid,
                Mobile: extInfo.mobile,
                UserName:'',
                UserIcon: '',
                FromID: uid,
                ShopID: shopid
              };
              util.request(api.WeChatLogin, pd, 'POST').then(function (res) {
                if (res.Status === 100) 
                {
                  wx.setStorageSync('userInfo', res.Data);
                  wx.reLaunch({url: '/pages/index/index'});                  
                } 
                else 
                {
                  wx.showModal({content: '1操作发生错误',success(res1) 
                    {
                      wx.reLaunch({url: '/pages/index/index'});
                    }
                  })

                }
              });
              //------------------结束注册---------------------------------
            } 
            else 
            {
              wx.showModal({content: '2操作发生错误',success(res1) 
                  {
                    wx.reLaunch({url: '/pages/index/index'});
                  }
                })
            }
          });
        },
        fail(err) 
        {
            wx.login({
                success: res => {
                    that.data.wx_login_code = res.code
                }
            })
        }
    })

  },
  update_user:function(e){
    var that = this;
    var tempInfo = e.detail.userInfo;
    that.setData({tempInfo: e.detail.userInfo});
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo != '') {
      userInfo.UserIcon = tempInfo.avatarUrl;
      userInfo.UserName = tempInfo.nickName;
      var pd = {
        OpenID: userInfo.OpenID,
        Mobile: userInfo.Mobile,
        UserName: tempInfo.nickName,
        UserIcon: tempInfo.avatarUrl
      };
      util.request(api.UpdateUserIcon, pd, 'POST').then(function (res) {
        if (res.Status === 100) 
        {
          that.setData({userInfo: userInfo});
          wx.setStorageSync('userInfo', userInfo);
        } 
        else 
        {
          wx.showModal({title: 'ERROR',content: '操作发生错误'});
        }
      });
    }
  }
})